package com.wyp168.serviceucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.commonutils.JwtUtils;
import com.wyp168.commonutils.MD5;
import com.wyp168.servicebase.exceptionhandler.GuliException;
import com.wyp168.serviceucenter.entity.UcenterMember;
import com.wyp168.serviceucenter.entity.vo.RegisterVo;
import com.wyp168.serviceucenter.mapper.UcenterMemberMapper;
import com.wyp168.serviceucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-05-24
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {

        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile)) {
            throw new GuliException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(queryWrapper);
        //判断手机号是否正确
        if(mobileMember == null) {  // 没有这个手机号
            throw new GuliException(20001,"查无此手机号");
        }
        //判断密码是否正确
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001,"密码错误");
        }
        //判断用户是否被禁用
        if(mobileMember.getIsDisabled()) {
            throw new GuliException(20001,"该用户被禁用");
        }

        //登录成功
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname) ||
            StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登录失败");
        }

        //判断手机验证码和redis中的手机验证码是否一样
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)) {
            throw new GuliException(20001,"验证码错误");
        }

        //判断手机号是否已被注册，如果已被注册则不添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new GuliException(20001,"手机号已被注册");
        }

        //数据添加到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setAvatar("https://guliedu-1115.oss-cn-hangzhou.aliyuncs.com/u%3D57925405%2C1694048582%26fm%3D253%26fmt%3Dauto%26app%3D138%26f%3DPNG.webp");
        member.setPassword(MD5.encrypt(password));
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getByOpenId(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);

        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegister(String day) {
        Integer count = baseMapper.countRegisterDay(day);
        return count;
    }
}
